package com.zzh.tools.utils.intervention;

import android.text.TextUtils;

/**
 * @author zhangzhihao
 * @email zhangzhihao@gongjiangren.net
 * @date 2020-12-18
 * @desc
 */
public class ToolImageAliOOS {

    private static final String oos_ali_ip = "http://image.gongjiangren.net/";

    /**
     * 依宽度为基础
     *
     * @param width
     * @param endUrl
     * @return
     */
    public static String getBasisWidth(int width, String endUrl) {
        return getBasis(true, width, endUrl);
    }

    /**
     * 依高度为基础
     *
     * @param height
     * @param endUrl
     * @return
     */
    public static String getBasisHeight(int height, String endUrl) {
        return getBasis(false, height, endUrl);
    }

    private static String getBasis(boolean isWidth, int length, String endUrl) {
        if (TextUtils.isEmpty(endUrl)) {
            return "";
        }
        if (endUrl.startsWith("http://") || endUrl.startsWith("https://")) {
            return endUrl;
        }
        if (length <= 0) {
            return new StringBuilder(oos_ali_ip).append(endUrl).toString();
        }
        return new StringBuilder(oos_ali_ip)
                .append(endUrl)
                .append("?x-oss-process=image/resize,")
                .append(isWidth ? "w_" : "h_")
                .append(length)
                .append(",m_lfit").toString();
    }

    /**
     * 对比宽度和高度选择基础
     *
     * @param width
     * @param height
     * @param endUrl
     * @return
     */
    public static String getBasisUnknown(int width, int height, String endUrl) {
        if (width > height) {
            return getBasisHeight(height, endUrl);
        } else {
            return getBasisWidth(width, endUrl);
        }
    }

    /**
     * oos 图片的ip
     *
     * @return
     */
    public static String getOosAliIp() {
        return oos_ali_ip;
    }

}
